package com.avic.rong.goodsSort.activity.secondListActivity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.bean.product.GoodBean;
import com.avic.rong.bean.product.SearchGroupBean;
import com.avic.rong.goodsSort.CarView;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.adapter.secondListAdapter.StandardItemAdapter;
import com.avic.rong.goodsSort.api.MuluSearchApi;
import com.avic.rong.goodsSort.api.StandardSearchApi;
import com.avic.rong.listener.EndlessRecyclerOnScrollListener;
import com.avic.rong.shoppingCar.activity.ShoppingCarActivity;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.EditUtils;
import com.avic.rong.utils.NetResponseListener;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.SmartHttp;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.customView.BookAttrFragment;
import com.avic.rong.wedgit.customView.DocumentAttrFragment;
import com.avic.rong.wedgit.customView.SearchFilterView;
import com.avic.rong.wedgit.customView.SoftwareAttrFragment;
import com.avic.rong.wedgit.customView.StandardAttrFragment;
import com.avic.rong.wedgit.customView.TypeGroupView;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.example.minato.minastore.utils.AppUtils;
import com.example.minato.minastore.utils.CircleProgressDialog;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsSortBzActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener, SearchFilterView.FilterInterface, StandardItemAdapter.CartAddListener {


    public static final int CODE_REQUEST = 1;
    public static final int ARG_PAGETYPE_SHOW_TYPE = 1000;
    public static final String ARG_PAGETYPE = "pageType";

    public static final String ARG_STANDARDTYPE = "standardType";
    public static final String ARG_SORT = "sort";
    public static final String ARG_CATID = "catId";
    public static final String ARG_KEYWORD = "keyword";
    public static final String ARG_ATTRPRO = "attrPro";
    public static final String ARG_MULU_SKIP = "mulu_skip";
    public static final int ARG_MULU_SKIP_API = 2000;
    public static final String ARG_MULU_ID = "mulu_id";


    @BindView(R.id.edit_keyword)
    EditText edit_keyword;//关键词
    @BindView(R.id.rb_sort_zh)
    RadioButton rb_sort_zh;
    @BindView(R.id.rb_sort_time)
    RadioButton rb_sort_time;
    @BindView(R.id.rb_sort_hot)
    RadioButton rb_sort_hot;
    @BindView(R.id.rb_sort_filter)
    RadioButton rb_sort_filter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;//loading
    @BindView(R.id.goods_sort_bz_DrawerLayout)
    DrawerLayout goods_sort_bz_DrawerLayout;
    @BindView(R.id.recycler_list)
    RecyclerView recycler_list;

    @BindView(R.id.frame_attr)
    FrameLayout frame_attr;
    @BindView(R.id.ll_group)
    LinearLayout ll_group;
    @BindView(R.id.filterView)
    SearchFilterView filterView;

    //type group start
    @BindView(R.id.ll_group_type)
    LinearLayout ll_group_type;
    @BindView(R.id.type_group_view)
    TypeGroupView type_group_view;//标准类型定位
    private int pageType = 0;


    private int standardType = 1;//1.商城标准
    private List<GoodBean> goodBeanList = new ArrayList<>();
    private CarView carView;
    private TextView cartNum;

    private StandardSearchApi standardSearchApi;
    private MuluSearchApi muluSearchApi;//目录搜索
    private Fragment currentFragment = new Fragment();
    private StandardItemAdapter standardItemAdapter;
    private int sort = 0;
    private String catIds = ConstantsUtils.EMPTY_STR;
    private String keyword;
    private String attrPro;
    private int muluApiUse;//控制从分类-标准目录跳转
    private int muluID;

    StandardAttrFragment standardAttrFragment;
    BookAttrFragment bookAttrFragment;
    DocumentAttrFragment documentAttrFragment;
    SoftwareAttrFragment softwareAttrFragment;
//    DividerItemDecoration divider;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_goods_sort_bz_two;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        keyword = getIntent().getStringExtra("keyword");
        standardType = getIntent().getIntExtra(ARG_STANDARDTYPE, 1);
        sort = getIntent().getIntExtra(ARG_SORT, 0);
        catIds = getIntent().getStringExtra(ARG_CATID);
        keyword = getIntent().getStringExtra(ARG_KEYWORD);
        pageType = getIntent().getIntExtra(ARG_PAGETYPE, 0);
        attrPro = getIntent().getStringExtra(ARG_ATTRPRO);
        muluApiUse = getIntent().getIntExtra(ARG_MULU_SKIP, 0);
        muluID = getIntent().getIntExtra(ARG_MULU_ID, 0);


        //加载购物车商品数量
        carView = new CarView(this);
        cartNum = carView.findViewById(R.id.car_number);
        carView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsSortBzActivity.this, ShoppingCarActivity.class);
                startActivity(intent);
            }
        });
        carView.show();

        goods_sort_bz_DrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loading_layout.setStatus(LoadingLayout.LOADING);
                isRefresh = false;
                currentPageNumber = currentPageNumber + 1;

                if (ARG_MULU_SKIP_API == muluApiUse) {
                    muluSearchApi.setPageNo(currentPageNumber);
                    HttpManager.getInstance().doHttpDeal(muluSearchApi);
                } else {
                    standardSearchApi.setPageNo(currentPageNumber);
                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                }
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });


        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        filterView.setFilterInterface(this);


        //recycler_list初始化
        LinearLayoutManager layoutManager = new LinearLayoutManager(GoodsSortBzActivity.this);
        recycler_list.setLayoutManager(layoutManager);
        endLessLoadMore(recycler_list, layoutManager);//预加载开启
//        divider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_05_pix));

        if (standardType == 1) {
            standardItemAdapter = new StandardItemAdapter(this, 1);
        } else if (standardType == 2) {
            standardItemAdapter = new StandardItemAdapter(this, 2);
//            recycler_list.addItemDecoration(divider);
        } else if (standardType == 3) {
            standardItemAdapter = new StandardItemAdapter(this, 3);
//            recycler_list.addItemDecoration(divider);
            carView.hide();
        } else if (standardType == 4) {
            standardItemAdapter = new StandardItemAdapter(this, 4);
//            recycler_list.addItemDecoration(divider);
            carView.hide();
        } else if (standardType == 5) {
            standardItemAdapter = new StandardItemAdapter(this, 5);
            carView.hide();
            filterView.hiddenFilter();//隐藏过滤图标
        }

        standardItemAdapter.setCartAddListener(this);
        recycler_list.setAdapter(standardItemAdapter);

        //列表数据请求--从mulu进来
        if (ARG_MULU_SKIP_API == muluApiUse) {
            muluSearchApi = new MuluSearchApi(GoodsSortBzActivity.this, muluSearchListener);
            muluSearchApi.setArgument(muluID, 0, null, null);
            HttpManager.getInstance().doHttpDeal(muluSearchApi);

            initStandardAttrFragment();
            switchFragment(standardAttrFragment);

        } else {
            standardSearchApi = new StandardSearchApi(GoodsSortBzActivity.this, searchAllListener);

            if (standardType == 1) {
                standardSearchApi.setArgument(1, 1, 0);
            } else if (standardType == 2) {
                standardSearchApi.setArgument(1, 2, 0);
            } else if (standardType == 3) {//实用文档
                standardSearchApi.setArgument(1, null, 0);
                standardSearchApi.setCatIds("2055");
            } else if (standardType == 4) {//软件工具
                standardSearchApi.setArgument(2, null, 0);
                standardSearchApi.setCatIds("456");
            } else if (standardType == 5) {//解决方案
                standardSearchApi.setArgument(2, null, 0);

            }
            standardSearchApi.setSort(sort);
//          if (sort != 0) {
            filterView.switchRadioButton(sort);
//          }
            if (!StringUtils.isEmpty(attrPro)) {
                standardSearchApi.setAttrPro(attrPro);
            }

            if (!StringUtils.isEmpty(catIds)) {
                standardSearchApi.setCatIds(catIds);
            }

            if (!StringUtils.isEmpty(keyword)) {
                standardSearchApi.setKeyword(keyword);
                edit_keyword.setText(keyword);
            }

            HttpManager.getInstance().doHttpDeal(standardSearchApi);


            if (pageType == ARG_PAGETYPE_SHOW_TYPE) {
                initStandardAttrFragment();
                switchFragment(standardAttrFragment);
                type_group_view.switchTypeButton(1);
//            initBookAttrFragment();
//            initDocumentAttrFragment();
//            initSoftwareAttrFragment();
            } else {
                //右侧抽屉视图
                if (standardType == 1) {//标准
                    initStandardAttrFragment();
                    switchFragment(standardAttrFragment);
                } else if (standardType == 2) {//图书
                    initBookAttrFragment(catIds);
                    switchFragment(bookAttrFragment);
                } else if (standardType == 3) {
                    initDocumentAttrFragment(catIds);
                    switchFragment(documentAttrFragment);
                } else if (standardType == 4) {
                    initSoftwareAttrFragment(catIds);
                    switchFragment(softwareAttrFragment);
                } else if (standardType == 5) {//解决方案

                }
            }
        }


        //初始化页面类型为type group页面的时候
        initTypeGroup();

        initEditListener();
        //获取购物车数量
        getCartNum();


    }


    private void initStandardAttrFragment() {
        standardAttrFragment = StandardAttrFragment.getInstance(this);
        standardAttrFragment.setClickInterface(new StandardAttrFragment.ClickInterface() {//抽屉OK确定
            @Override
            public void clickOK(String attrPro) {
                isRefresh = true;
                currentPageNumber = 1;
                if (ARG_MULU_SKIP_API == muluApiUse) {
                    muluSearchApi.setPageNo(currentPageNumber);
                    muluSearchApi.setAttrPro(attrPro);
                    HttpManager.getInstance().doHttpDeal(muluSearchApi);
                } else {
                    standardSearchApi.setPageNo(currentPageNumber);
                    standardSearchApi.setAttrPro(attrPro);
                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                }

                CircleProgressDialog.showDialog(context);
                if (EditUtils.isSoftShowing(GoodsSortBzActivity.this)) {
                    EditUtils.hideSystemSoftKeyboard(GoodsSortBzActivity.this);
                }
                goods_sort_bz_DrawerLayout.closeDrawers();
            }
        });
    }

    private void initBookAttrFragment(String catId) {
        if (bookAttrFragment == null) {
            bookAttrFragment = BookAttrFragment.getInstance(this, catId);
            bookAttrFragment.setClickInterface(new BookAttrFragment.ClickInterface() {
                @Override
                public void clickOK(String attrPro) {
                    String catIds = "";
                    String tempAttrPro = "";
                    if (!StringUtils.isEmpty(attrPro)) {
                        String[] attrArray = attrPro.split("&");
                        if (attrPro.contains("10000")) {
                            catIds = attrArray[0].substring(attrArray[0].indexOf("10000++") + 7, attrArray[0].lastIndexOf("__"));
                            for (int index = 1; index < attrArray.length; index++) {
                                tempAttrPro = tempAttrPro + "&" + attrArray[index];
                            }
                        } else {
                            for (int index = 0; index < attrArray.length; index++) {
                                tempAttrPro = tempAttrPro + "&" + attrArray[index];
                            }
                        }


                        if (!StringUtils.isEmpty(tempAttrPro)) {
                            tempAttrPro = tempAttrPro.substring(1);
                        }
                    }
                    isRefresh = true;
                    currentPageNumber = 1;
                    standardSearchApi.setPageNo(currentPageNumber);
//                if (!StringUtils.isEmpty(tempAttrPro)) {
                    standardSearchApi.setAttrPro(tempAttrPro);
//                }
//                if (!StringUtils.isEmpty(catIds)) {
                    System.out.println("------------------------------000:" + catIds);
                    standardSearchApi.setCatIds(catIds);
//                }
                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                    CircleProgressDialog.showDialog(context);
                    if (EditUtils.isSoftShowing(GoodsSortBzActivity.this)) {
                        EditUtils.hideSystemSoftKeyboard(GoodsSortBzActivity.this);
                    }
                    goods_sort_bz_DrawerLayout.closeDrawers();
                }
            });
        }


    }


    private void initDocumentAttrFragment(String catId) {
        documentAttrFragment = DocumentAttrFragment.getInstance(this, catId);
        documentAttrFragment.setClickInterface(new DocumentAttrFragment.ClickInterface() {//抽屉OK确定
            @Override
            public void clickOK(String attrPro) {
                isRefresh = true;
                currentPageNumber = 1;
                standardSearchApi.setPageNo(currentPageNumber);
                standardSearchApi.setCatIds(attrPro);
                HttpManager.getInstance().doHttpDeal(standardSearchApi);
                CircleProgressDialog.showDialog(context);
                if (EditUtils.isSoftShowing(GoodsSortBzActivity.this)) {
                    EditUtils.hideSystemSoftKeyboard(GoodsSortBzActivity.this);
                }
                goods_sort_bz_DrawerLayout.closeDrawers();
            }
        });

    }


    private void initSoftwareAttrFragment(String catId) {
        softwareAttrFragment = SoftwareAttrFragment.getInstance(this, catId);
        softwareAttrFragment.setClickInterface(new SoftwareAttrFragment.ClickInterface() {//抽屉OK确定
            @Override
            public void clickOK(String attrPro) {
                String catIds = "";
                String tempAttrPro = "";
                if (!StringUtils.isEmpty(attrPro)) {
                    String[] attrArray = attrPro.split("&");

                    if (attrPro.contains("50000")) {
                        catIds = attrArray[0].substring(attrArray[0].indexOf("50000++") + 7, attrArray[0].lastIndexOf("__"));
                        for (int index = 1; index < attrArray.length; index++) {
                            tempAttrPro = tempAttrPro + "&" + attrArray[index];
                        }
                    } else {
                        for (int index = 0; index < attrArray.length; index++) {
                            tempAttrPro = tempAttrPro + "&" + attrArray[index];
                        }
                    }


                    if (!StringUtils.isEmpty(tempAttrPro)) {
                        tempAttrPro = tempAttrPro.substring(1);
                    }
                }


                isRefresh = true;
                currentPageNumber = 1;
                standardSearchApi.setPageNo(currentPageNumber);
                if (!StringUtils.isEmpty(catIds)) {
                    standardSearchApi.setCatIds(catIds);
                } else {
                    standardSearchApi.setCatIds("459");
                }
//                if (!StringUtils.isEmpty(tempAttrPro)) {
                standardSearchApi.setAttrPro(tempAttrPro);
//                }
                HttpManager.getInstance().doHttpDeal(standardSearchApi);
                CircleProgressDialog.showDialog(context);
                if (EditUtils.isSoftShowing(GoodsSortBzActivity.this)) {
                    EditUtils.hideSystemSoftKeyboard(GoodsSortBzActivity.this);
                }
                goods_sort_bz_DrawerLayout.closeDrawers();
            }
        });

    }

    /**
     * pageTypeGroup页面的时候，从搜索栏过来
     */
    private void initTypeGroup() {
        if (pageType == ARG_PAGETYPE_SHOW_TYPE) {
            ll_group_type.setVisibility(View.VISIBLE);
            type_group_view.setGroupTypeInterface(new TypeGroupView.GroupTypeInterface() {
                @Override
                public void clickType(int type) {
                    isRefresh = true;
                    currentPageNumber = 1;
                    standardSearchApi.setPageNo(currentPageNumber);
                    if (type == 1) {
                        standardType = 1;
                        standardSearchApi.setArgument(1, 1, 0);

                        if (standardAttrFragment == null) {
                            initStandardAttrFragment();
                        }
                        switchFragment(standardAttrFragment);
                    } else if (type == 2) {
                        standardType = 2;
                        standardSearchApi.setArgument(1, 2, 0, keyword, null, null);

                        if (bookAttrFragment == null) {
                            initBookAttrFragment(null);
                        }
//                        recycler_list.addItemDecoration(divider);
                        switchFragment(bookAttrFragment);
                    } else if (type == 3) {//实用文当
                        standardType = 3;
                        standardSearchApi.setArgument(1, null, 0, keyword, "2055", null);

                        if (documentAttrFragment == null) {
                            initDocumentAttrFragment(null);
                        }
//                        recycler_list.addItemDecoration(divider);
                        switchFragment(documentAttrFragment);
                    } else if (type == 4) {//软件工具
                        standardType = 4;
                        standardSearchApi.setArgument(2, null, 0, keyword, "459", null);

                        if (softwareAttrFragment == null) {
                            initSoftwareAttrFragment(null);
                        }
                        switchFragment(softwareAttrFragment);
//                        recycler_list.addItemDecoration(divider);
                    }
                    standardItemAdapter.setStandardType(standardType);


                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                }
            });
        } else {
            ll_group_type.setVisibility(View.GONE);
        }
    }


    /**
     * 顶部Edit搜索
     */
    private void initEditListener() {
        edit_keyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {

                    //关闭软键盘
                    EditUtils.showORhideSoftKeyboard(GoodsSortBzActivity.this);
                    keyword = edit_keyword.getText().toString();
                    if (StringUtils.isEmpty(keyword)) {
                        ToastTools.showToast("请输入关键词!");
                        return true;
                    }
                    currentPageNumber = 1;
//                    standardSearchApi.setSort(0);
                    standardSearchApi.setKeyword(keyword.trim());
                    standardSearchApi.setPageNo(currentPageNumber);
                    isRefresh = true;
                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                    return true;
                }
                return false;
            }
        });
    }


    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> searchAllListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            CircleProgressDialog.closeDialog();
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())) {
                ToastTools.showToast(GoodsSortBzActivity.this, subjects.getMsg());
                Logger.e("cjx", "biaozhun数据：" + subjects.getMsg());
                return;
            }
            List<SearchGroupBean> beans = (List<SearchGroupBean>) subjects.getData();
            if (isRefresh) {
                goodBeanList.clear();
            }

            if (standardType == 1) {
                goodBeanList.addAll(beans.get(0).getData());
            } else if (standardType == 2 || standardType == 3 || standardType == 4 || standardType == 5) {
                goodBeanList.addAll(beans.get(1).getData());
            }


            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

            if (goodBeanList.size() == 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            standardItemAdapter.setBeanList(goodBeanList);
        }


        @Override
        public void onError(final Throwable e) {
            CircleProgressDialog.closeDialog();
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
            Logger.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> muluSearchListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            CircleProgressDialog.closeDialog();
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())) {
                ToastTools.showToast(GoodsSortBzActivity.this, subjects.getMsg());
                Logger.e("cjx", "biaozhun数据：" + subjects.getMsg());
                return;
            }
            List<GoodBean> beanList = (List<GoodBean>) subjects.getData();
            if (isRefresh) {
                goodBeanList.clear();
            }

            goodBeanList.addAll(beanList);

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

            if (goodBeanList.size() == 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            standardItemAdapter.setBeanList(goodBeanList);


        }


        @Override
        public void onError(final Throwable e) {
            CircleProgressDialog.closeDialog();
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
            Logger.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };


    /**
     * 获取购物车数量
     */
    private void getCartNum() {
        String tokenStr = ConstantsUtils.EMPTY_STR;
        try {
            tokenStr = URLEncoder.encode(SharedPreferencesUtil.getLoginToken(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String urlStr = AppUtils.BASE_URL + "cart/getCartNum?token=" + tokenStr + "&deviceId=" + DeviceUtils.getIMEString(context);
        SmartHttp.getHttp(urlStr, new NetResponseListener() {
            @Override
            public void sucListener(JSONObject response) {
                if (ConstantsUtils.STATUS_SUCCESS_0000.equals(response.optString("code"))) {
                    cartNum.setText(response.optString("data"));
                }
            }

            @Override
            public void errListener(String error_msg) {

            }
        });
    }


    /**
     * 校验库存
     *
     * @param num
     * @param skuId
     */
    private void checkGoodsNum(int num, int skuId) {
        String urlStr = AppUtils.BASE_URL + "cart/checkGoodsNum?num" + num + "&skuId=" + skuId;
        SmartHttp.getHttp(urlStr, new NetResponseListener() {
            @Override
            public void sucListener(JSONObject response) {
                if (ConstantsUtils.STATUS_SUCCESS_0000.equals(response.optString("code"))) {
                    cartNum.setText(response.optString("data"));
                } else if (ConstantsUtils.STATUS_SYSTEM_ERROR_9999.equals(response.optString("code"))) {
                    ToastTools.showToast("库存不足!");
                } else {
                    ToastTools.showToast("系统异常");
                }
            }

            @Override
            public void errListener(String error_msg) {
                ToastTools.showToast(error_msg);
            }
        });
    }


    @OnClick({R.id.img_category, R.id.img_back, R.id.img_delete})
    public void clickListener(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.img_back:
                onBackPressed();
                break;
            case R.id.img_category:
                startActivity(GoodsSortActivity.getIntent(context));
                break;
            case R.id.img_delete://清空关键词
                edit_keyword.setText(ConstantsUtils.EMPTY_STR);
                keyword = ConstantsUtils.EMPTY_STR;
                standardSearchApi.setKeyword(keyword);
                break;
        }
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        if (ARG_MULU_SKIP_API == muluApiUse) {
            muluSearchApi.setPageNo(currentPageNumber);
            HttpManager.getInstance().doHttpDeal(muluSearchApi);
        } else {
            standardSearchApi.setPageNo(currentPageNumber);
            HttpManager.getInstance().doHttpDeal(standardSearchApi);
        }

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        System.out.println("------------------------------onrefresh:");
        isRefresh = true;
        currentPageNumber = 1;
        if (ARG_MULU_SKIP_API == muluApiUse) {
            muluSearchApi.setPageNo(currentPageNumber);
            HttpManager.getInstance().doHttpDeal(muluSearchApi);
        } else {
            standardSearchApi.setPageNo(currentPageNumber);
            HttpManager.getInstance().doHttpDeal(standardSearchApi);
        }

    }


    @Override
    public void clickSort(int sort) {//排序监听
        isRefresh = true;
        currentPageNumber = 1;
        if (ARG_MULU_SKIP_API == muluApiUse) {
            muluSearchApi.setPageNo(currentPageNumber);
            muluSearchApi.setSort(sort);
        } else {
            standardSearchApi.setPageNo(currentPageNumber);
            standardSearchApi.setSort(sort);
            HttpManager.getInstance().doHttpDeal(standardSearchApi);
        }
    }

    @Override
    public void openFilter() {//打开抽屉
        goods_sort_bz_DrawerLayout.openDrawer(GravityCompat.END);
    }


    //添加购物车
    @Override
    public void addCar(int skuId, ImageView cartImageView) {
        Map<String, String> map = new HashMap<>();
        String skuStr = "{" + "\"" + skuId + "\"" + ":" + "1}";
        map.put("skuStr", skuStr);
        map.put("orderType", "4");
        map.put("buyAgain", "0");//是否再次购买（0/1）
        map.put("token", SharedPreferencesUtil.getLoginToken());
        map.put("deviceId", DeviceUtils.getIMEString(context));
        AddCarApi addCarApi = new AddCarApi(GoodsSortBzActivity.this, new AddCarOnNextListener(cartImageView));
        addCarApi.setCache(false);
        addCarApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(addCarApi);
    }


    class AddCarOnNextListener extends HttpOnNextListener<BaseResultEntity> {

        ImageView cartImageView;

        public AddCarOnNextListener(ImageView cartImg) {
            this.cartImageView = cartImg;
        }

        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!ConstantsUtils.STATUS_SUCCESS_0000.equals(subjects.getCode())) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
            double currentCarNumber = (double) subjects.getData();
            cartNum.setText(String.valueOf(((int) currentCarNumber)));
            Logger.e("------------------currentCarNumber:" + String.valueOf(currentCarNumber));
            //动画
            addProductionAnimation(cartImageView, carView);
        }

        @Override
        public void onError(final Throwable e) {
            ToastTools.showToast(context, e.getMessage());
        }
    }


    /**
     * 预加载
     *
     * @param recyclerView
     * @param layoutManager
     */
    private void endLessLoadMore(RecyclerView recyclerView, LinearLayoutManager layoutManager) {
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                isRefresh = false;
                currentPageNumber = currentPageNumber + 1;

                if (ARG_MULU_SKIP_API == muluApiUse) {
                    muluSearchApi.setPageNo(currentPageNumber);
                    HttpManager.getInstance().doHttpDeal(muluSearchApi);
                } else {
                    standardSearchApi.setPageNo(currentPageNumber);
                    HttpManager.getInstance().doHttpDeal(standardSearchApi);
                }
            }
        });
    }


    private void switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {//如果要显示的targetFragment没有添加过
            transaction
                    .hide(currentFragment)//隐藏当前Fragment
                    .add(R.id.frame_attr, targetFragment, targetFragment.getClass().getName())//添加targetFragment
                    .commit();
        } else {//如果要显示的targetFragment已经添加过
            transaction//隐藏当前Fragment
                    .hide(currentFragment)
                    .show(targetFragment)//显示targetFragment
                    .commit();
        }
        //更新当前Fragment为targetFragment
        currentFragment = targetFragment;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (carView != null) {
            carView.destory();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == CODE_REQUEST && resultCode == 100) {
            boolean addCartFlag = data.getBooleanExtra(DetailProductActivity.TYPE_ADDCARTFLAG, false);
            if (addCartFlag) {
                getCartNum();
            }
        }
    }

    public static Intent getIntent(Context context, int standardType) {
        Intent intent = new Intent(context, GoodsSortBzActivity.class);
        intent.putExtra(ARG_STANDARDTYPE, standardType);
        return intent;
    }


    public static Intent getIntent(Context context, int standardType, int sort, String catId, String attrPro) {
        Intent intent = new Intent(context, GoodsSortBzActivity.class);
        intent.putExtra(ARG_STANDARDTYPE, standardType);
        intent.putExtra(ARG_SORT, sort);
        intent.putExtra(ARG_CATID, catId);
        intent.putExtra(ARG_ATTRPRO, attrPro);
        return intent;
    }


    public static Intent getIntent(Context context, int standardType, String keyword) {
        Intent intent = new Intent(context, GoodsSortBzActivity.class);
        intent.putExtra(ARG_STANDARDTYPE, standardType);
        intent.putExtra(ARG_KEYWORD, keyword);
        return intent;
    }

    /**
     * @param context
     * @param keyword
     * @param pageType 页面类型
     * @return
     */
    public static Intent getIntent(Context context, String keyword, int pageType) {
        Intent intent = new Intent(context, GoodsSortBzActivity.class);
        intent.putExtra(ARG_KEYWORD, keyword);
        intent.putExtra(ARG_PAGETYPE, pageType);
        return intent;
    }

    /**
     * mulu 目录跳转专用
     *
     * @param context
     * @param mulu
     * @return
     */
    public static Intent getIntent(Context context, String mulu) {
        Intent intent = new Intent(context, GoodsSortBzActivity.class);
        intent.putExtra(ARG_MULU_SKIP, ARG_MULU_SKIP_API);
        intent.putExtra(ARG_MULU_ID, Integer.parseInt(mulu));
        return intent;
    }


    @Override
    protected void addListener() {

    }


}
